var AuthFunc = function() {

    var urls = {
        index: 'index',
        cs: 'cs',
        del: 'del',
        edit: 'edit',
        add: 'add',
    }

    var $table = $('#list-container')

    var tableInit = function () {
        $table.bootstrapTable('destroy');
        var columns = [
            {title: 'ID', field: 'id'},
            {title: '名称', field: 'name', editable: {
                type: 'text',
                validate: function (v) {
                    if(v.length<1) return '不能为空';
                }
            }},
            {title: '路由', field: 'router', editable: {
                type: 'text',
                validate: function (v) {
                    if(v.length<1) return '不能为空';
                }
            }},
            {title: '状态', field: 'is_disabled', formatter: function(value, row, index){
                if (value == 1)
                    return '<span class="badge badge-success">是</span>'
                else
                    return '<span class="badge badge-grey">否</span>'
            }},
            {title: '状态', field: 'status', width: '80px', formatter: function(value, row, index){
                return ace.helper.renderSwitch(value == 1)
                if (value == 1)
                    return '<span class="badge badge-success">启用</span>'
                else
                    return '<span class="badge badge-grey">禁用</span>'
            }},
            {title: '备注', field: 'intro'},
            {title: '最后修改时间', field: 'updated_at', width: '160px'},
            {title: '操作', width: '220px', cellStyle: {
                css: {'max-width': '220px'},
            },class: 'actions', formatter: function(value, row, index) {
                var html = '<a href="'+urls.edit+'?id='+row.id+'" isIframe width="700px" height="450px">编辑</a>';
                if (row.status==1) {
                    html += '<a onclick="cs('+row.id+', 2)">下架</a>'
                } else {
                    html += '<a onclick="cs('+row.id+', 1)">禁用</a>';
                }
                html += '<a onclick="del('+row.id+')">删除</a>';
                if (row.level < 3) {
                    html += '<div class="inline more-actions"><div class="dropdown">'+
                            '<button class="btn dropdown-toggle" data-toggle="dropdown">更多操作<span class="caret"></span></button>'+
                            '<ul class="dropdown-menu pull-right" style="min-width: 80px">';
                        html += '<li><a href="'+urls.add+'?pid='+row.id+'" width="700px" height="450px" isIframe>添加子菜单</a></li>'
                        html += '</ul></div></div>'
                }
                return html;
            }}
        ]
        $table.bootstrapTable({
            url: urls.index,
            detailView: true,
            pagination: false,
            columns: columns,
            queryParams: function (params){
                return $.extend({}, params, ace.helper.serializeForm('#table-search form'))
            },
            onEditableSave: function (field, row, index, oldValue, ele) {
                ace.helper.tableEdit(field, row, index, oldValue, ele, urls.edit, t)
            },
        });
    }

    var saveFromValidateInit = function () {
        
    }
    return {

        init: function() {
            tableInit();
        }

    };
}();

jQuery(document).ready(function() {
    AuthFunc.init();
});

var table_container = '#list-container'
initTable()
function refreshTable() {
    var querys = ace.helper.serializeForm('.table-search form')
    $(table_container).ace_table('refresh', {
        query: querys
    });
}
function cs(id, status) {
    ace.helper.ajax(urls.cs, {id: id, status: status}, function() {
        refreshTable()
    })
}
function del(id) {
    ace.helper.confirm('确定要删除吗？', function(){
        ace.helper.ajax(urls.del, {id: id}, function() {
            refreshTable()
        })
    })
}

function initTable () {
    
}
$('#saveForm').aceValidate({
    rules: {
        title: {required: true, minlength: 2, maxlength: 20},
    },
    messages: {
        name: {required: "菜单名称不能为空"},
    }
})

jQuery(function($) {

    // $('input[name=open_time]').timepicker({
    //     defaultTime: '08:00',
    //     minuteStep: 10,
    //     showSeconds: false,
    //     showMeridian: false,
    //     disableFocus: true,
    //     icons: {
    //         up: 'fa fa-chevron-up',
    //         down: 'fa fa-chevron-down'
    //     }
    // })
    // $('input[name=close_time]').timepicker({
    //     defaultTime: '20:00',
    //     minuteStep: 10,
    //     showMeridian: false,
    //     disableFocus: true,
    //     icons: {
    //         up: 'fa fa-chevron-up',
    //         down: 'fa fa-chevron-down'
    //     }
    // })
});

// ace.upload.singleImg.init({
//     pick: '#coverImgPicker',
//     filename: 'shop_cover',
//     success: function (file, res) {
//         if (res.code == 0) {
//             $('input[name=cover_url]').val(res.data.filepath)
//             $('#shopCoverBox .imgItem').css('background-image', 'url('+res.data.fileUrl+')')
//             $('#shopCoverBox').show()
//             $('input[name=cover_img]').closest('.form-group').find('.help-block').remove();
//         } else {
//             layer.msg(res.msg, {icon: 2});
//         }
//     }
// })